//app.js
App({
  onLaunch: function () {
    this.loginUser();
  },
  loginUser: function (param) {
    console.log(param)
    // if (this.globalData.logining) {
    //   return;
    // }

    let app = this;
    app.globalData.logining = true
    console.log('登录')
    wx.login({
      success: res => {
        if (this.globalData.show) {
          console.log('展示，拒绝访问')
          return;
        }
        console.log('获取code')
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: app.globalData.requestUrl + "/api/v1/login",
          data: {
            code: res.code
          },
          success: function (res) {
            let ret = res.data;
            console.log(ret)
            if (ret.status == 200) {
              app.globalData.login = ret.data.login;
              app.globalData.authorised = ret.data.authorised;
              app.globalData.header.Cookie = 'JSESSIONID=' + ret.data.sessionid;
              app.globalData.logining = false
              console.log(app.globalData)
            }
            if (param && param.success) {
              console.log('执行回调')
              param.success()
            }
            if (app.authoriseCheck) {
              app.authoriseCheck(ret.data.authorised)
            }
            if (app.globalData.authorised) {
              app.globalData.socket = wx.connectSocket({
                url: app.globalData.wsUrl,
                header: app.globalData.header,
                success: function (res) {
                  console.log('websocket connect success')
                }
              })
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    header: {

    },
    requestUrl: 'http://localhost:8080',
    wsUrl: 'ws://localhost:8080/ws/room',
    login: false
  }
})